#include "RWLock.h"
#include <pthread.h>
//#include <pthread.cpp>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <time.h>

RWLock RWLOCK = RWLock();

void *pedir_read(void *null){
	printf("Llamo al rlock\n");
	RWLOCK.rlock();
	printf("puedo leer\n");
	sleep(1);
	printf("termine de leer\n");
	RWLOCK.runlock();
	pthread_exit(NULL);
}


void * pedir_write(void *null){
	printf("Llamo al wlock\n");
	RWLOCK.wlock();
	printf("puedo escribir\n");
	sleep(1);
	printf("termine de escribir\n");
	RWLOCK.wunlock();
	pthread_exit(NULL);
}


int main(int argc, char *argv[]){
    srand(time(NULL));
	for(int i=0; i<1000; i++){
        pthread_t tid;
        int rc;
        if(rand()%2 ==1){
            rc = pthread_create(&tid, NULL,pedir_read, NULL);
        }
        else{
            rc = pthread_create(&tid,NULL,pedir_write, NULL);
        }
	}
    sleep(1000);
	return 0;
}
